﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DCTLib.Common;
using System.Data;

namespace DCTAccess.Business.Member
{
    public class MemberFilter
    {
        protected int _ID = -1;
        protected string _userName = string.Empty;
        protected string _hashedPassword = string.Empty;
        protected string _fullName = string.Empty;
        protected string _email = string.Empty;
        protected string _mobile = string.Empty;
        protected string _address = string.Empty;
        protected string _birthday = string.Empty;
        protected string _lastLoginDate = string.Empty;
        protected string _regDate;
        protected string _uniqueCode = string.Empty;
        protected int _levelInfo;
        private string _orderByFullName = string.Empty;
        private bool _active = false;
               
        #region Properties
        public bool Active
        {
            get { return _active; }
            set { _active = value; }
        }

        public string OrderByFullName
        {
            get { return _orderByFullName; }
            set { _orderByFullName = value; }
        }

        public int LevelInfo
        {
            get { return _levelInfo; }
            set { _levelInfo = value; }
        }

        public string UniqueCode
        {
            get { return _uniqueCode; }
            set { _uniqueCode = value; }
        }

        public string RegDate
        {
            get { return _regDate; }
            set { _regDate = value; }
        }

        public string Birthday
        {
            get { return _birthday; }
            set { _birthday = value; }
        }
        public string Address
        {
            get { return _address; }
            set { _address = value; }
        }


        public string Mobile
        {
            get { return _mobile; }
            set { _mobile = value; }
        }

        public int ID
        {
            get { return _ID; }
            set { _ID = value; }
        }

        public string UserName
        {
            get { return _userName; }
            set { _userName = value; }
        }

        public string HashedPassword
        {
            get { return _hashedPassword; }
            set { _hashedPassword = value; }
        }

        public string FullName
        {
            get { return _fullName; }
            set { _fullName = value; }
        }

        public string Email
        {
            get { return _email; }
            set { _email = value; }
        }

        public string LastLoginDate
        {
            get { return _lastLoginDate; }
            set { _lastLoginDate = value; }
        }

       
        #endregion 

        public static bool UpdateLastLoginDate(Member member)
        {
            return ExecuteStoredProcedure(member, "UpdateLastLoginDate");
        }

        private string SelectAllSQL
        {
            get
            {
                StringBuilder sql = new StringBuilder();
                sql.Append("SELECT MemberID, Username, FullName, HashedPassword, Mobile, Address, Email, Birthday ,RegDate, LastLoginDate , UniqueCode, LevelInfo ");
                sql.Append("FROM Web_Members ");
                sql.Append("WHERE [Active] = 1 ");

                return sql.ToString();
            }
        }

        public DataTable GetMember()
        {
            SQLHelper sqlHelper = new SQLHelper();
            sqlHelper.SwitchCommandType(CommandType.Text);

            StringBuilder sql = new StringBuilder(SelectAllSQL);

            if (ID != -1)
            {
                sql.Append("AND " + IntegerCompareSQLBuilder.GetSQL("MemberID", IntegerCompareTypes.Equals, "@MemberID"));
                sqlHelper.AddParameter("@MemberID", ID);
            }

            if (UserName.Trim().Length > 0)
            {
                sql.Append("AND " + StringCompareSQLBuilder.GetSQL("Username", StringCompareTypes.Equals, "@Username"));
                sqlHelper.AddParameter("@Username", UserName);
            }

            if (HashedPassword.Trim().Length > 0)
            {
                sql.Append("AND " + StringCompareSQLBuilder.GetSQL("HashedPassword", StringCompareTypes.Equals, "@HashedPassword"));
                sqlHelper.AddParameter("@HashedPassword", HashedPassword);
            }

            if (FullName.Trim().Length > 0)
            {
                sql.Append("AND " + StringCompareSQLBuilder.GetSQL("FullName", StringCompareTypes.Contains, "@FullName", true));
                sqlHelper.AddParameter("@FullName", FullName.ToLower());
            }

            if (UniqueCode.Trim().Length > 0)
            {
                sql.Append("AND " + StringCompareSQLBuilder.GetSQL("UniqueCode", StringCompareTypes.Contains, "@Unique", true));
                sqlHelper.AddParameter("@Unique", UniqueCode.ToLower());
            }

            if (OrderByFullName.Length > 0)
            {
                sql.Append(OrderByFullName);
                return sqlHelper.GetDataTable(sql.ToString());
            }

            return sqlHelper.GetDataTable(sql.ToString() + " ORDER BY Username ASC  ");
        }

        private static bool ExecuteStoredProcedure(Member member, string action)
        {
            SQLHelper sqlHelper = new SQLHelper();
            string sProc = "Members_spr";
            sqlHelper.AddParameter("@Action", action);
            sqlHelper.AddParameter("@MemberID", member.ID);
            sqlHelper.AddParameter("@Username", member.UserName);
            sqlHelper.AddParameter("@HashedPassword", member.HashedPassword);
            sqlHelper.AddParameter("@FullName", member.FullName);
            sqlHelper.AddParameter("@Mobile", member.Mobile);
            sqlHelper.AddParameter("@Address", member.Address);
            sqlHelper.AddParameter("@Email", member.Email);
            sqlHelper.AddParameter("@UniqueCode", member.UniqueCode);
            sqlHelper.AddParameter("@LevelInfo", member.LevelInfo);
            
            return sqlHelper.ExecuteSQL(sProc);
        }
    }
}
